package com.smcdd.cdb.dbo.cdb_dbo.ui.activity;

import android.widget.ImageView;

import com.smcdd.cdb.dbo.cdb_dbo.R;
import com.smcdd.cdb.dbo.cdb_dbo.service.presenter.LamplightPresenter;
import com.smcdd.cdb.dbo.cdb_dbo.service.view.ILamplightAc;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BaseActivity;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ToastUtil;


/**
 * 作者：Administrator on 2017/7/4 15:58
 * 邮箱：zhanghuaiha@gmail.com
 * 灯光控制
 */

public class LamplightActivity extends BaseActivity<ILamplightAc,LamplightPresenter> implements ILamplightAc {
    private ImageView lamp_back,lamp_open,lamp_flicker,lamp_close;
    private String alias;

    @Override
    public void initView() {
        lamp_back = (ImageView) findViewById(R.id.lamp_back);
        lamp_open = (ImageView) findViewById(R.id.lamp_open);
        lamp_flicker = (ImageView) findViewById(R.id.lamp_flicker);
        lamp_close = (ImageView) findViewById(R.id.lamp_close);
    }

    @Override
    public void initData() {
        alias = getIntent().getStringExtra("alias");
        mPresenter.loadDate(alias);
    }

    @Override
    public void initListener() {
        lamp_back.setOnClickListener(v -> finish());
        lamp_open.setOnClickListener(v -> {
            if (lamp_open.isSelected()) {
                return;
            }else {
                mPresenter.changeState(alias,"2");
                lamp_open.setSelected(true);
                lamp_flicker.setSelected(false);
                lamp_close.setSelected(false);
            }
        });

        lamp_flicker.setOnClickListener(v -> {
            if (lamp_flicker.isSelected()) {
                return;
            }else {
                mPresenter.changeState(alias,"1");
                lamp_open.setSelected(false);
                lamp_flicker.setSelected(true);
                lamp_close.setSelected(false);
            }
        });

        lamp_close.setOnClickListener(v -> {
            if (lamp_close.isSelected()) {
                return;
            }else {
                mPresenter.changeState(alias,"0");
                lamp_open.setSelected(false);
                lamp_flicker.setSelected(false);
                lamp_close.setSelected(true);
                ToastUtil.showToast("点击关闭后，绿灯闪烁8次后关闭");
            }
        });

    }

    @Override
    protected LamplightPresenter createPresenter() {
        return new LamplightPresenter(this);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_lamp;
    }

    @Override
    public ImageView getOpen() {
        return lamp_open;
    }

    @Override
    public ImageView getFlicker() {
        return lamp_flicker;
    }

    @Override
    public ImageView getClose() {
        return lamp_close;
    }
}
